package com.ximu.test.filter;


import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class Image implements Filter{
	
	private  FilterConfig filterConfig;
	
	@Override
	public void destroy() {
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest request = (HttpServletRequest) req;
		String referer = request.getHeader("referer");
		if(filterConfig.getInitParameter("filterImage").equals("true")&&
				(referer==null||!referer.contains(request.getServerName()))){
			request.getRequestDispatcher("/image/error.gif").forward(req, res);
		}else{
			chain.doFilter(req, res);
		}
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig = filterConfig;
	}	
}
